package cn.kgm.callback.net;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2024-3-19 15:00
 * @Description: cn.kgm.callback.net.Server
 */
public class Server {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        Server server = new Server();

        // 模拟客户端请求
        Callback callback = new Callback() {
            @Override
            public void onComplete(String result) {
                System.out.println("客户端收到回调: " + result);
            }
        };

        // 异步处理请求
        executor.submit(() -> server.handleRequest(callback));
    }

    public void handleRequest(Callback callback) {
        // 模拟耗时操作
        System.out.println("服务器开始处理请求..." );
        try {
            Thread.sleep(2000); // 假设这是一些耗时的处理
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("服务器处理完成。" );

        // 调用回调函数
        if (callback != null) {
            callback.onComplete("请求处理结果" );
        }
    }

    interface Callback {
        void onComplete(String result);
    }
}
